<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Index_Base extends Controller
{
    //protected $_title  = 'StyleMay';
	protected $_layout = 'index/layout_tpl';
    protected $_need_auth = false;
    
    protected function _is_need_auth()
    {
        return $this->_need_auth;
    }
    
    public function before()
    {
        parent::before();
        
        if (!Model_Auth::instance()->logged_in() && $this->_is_need_auth()) {
            Request::current()->redirect('/login');    
        }
         
        $this->_get_params();   
    }
    
    protected function _get_params()
    {
        $changed = false;
        $url     = $_SERVER['REQUEST_URI'];
        if (isset($_GET['subid']) && ($subid = $_GET['subid'])) {
            //$this->_redirect_if_neccesary($subid);
            setcookie('subid', $subid, time()+30*24*60*60, '/');     // 30 дней 
            
            $url = str_replace(array('?subid='.$subid, '&subid='.$subid), array('', ''), $url); 
            $changed = true;
        }
        if (isset($_GET['utm_source']) && $_GET['utm_source'] == 'recommend' &&
            isset($_GET['utm_medium']) && $_GET['utm_medium'] == 'referral' &&
            isset($_GET['utm_campaign']) && ($recommend = $_GET['utm_campaign'])) {
            setcookie('recommend', $recommend, time()+3*30*24*60*60, '/');     // 90 дней 
        }
        /*if (isset($_GET['cat']) && ($cat = $_GET['cat'])) {
            $session = Session::instance();
            $session->set('category-redirect', $cat);
            
            $url = str_replace(array('?cat='.$cat, '&cat='.$cat), array('', ''), $url);
            $changed = true;
        }  */
        
        if ($changed) {
            Helper_Index::redirect_301($url);    
        }
    }
    
    /*protected function _redirect_if_neccesary($subid)
    {
        $links = array(
            'svitera'       => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/301/women.aspx?sort=popular&pagesize=100',
            'djinsyi'       => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/%D0%94%D0%BB%D1%8F_%D0%B6%D0%B5%D0%BD%D1%89%D0%B8%D0%BD/%D0%9E%D0%B4%D0%B5%D0%B6%D0%B4%D0%B0/%D0%94%D0%B6%D0%B8%D0%BD%D1%81%D1%8B',
            'kardiganyi'    => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/303/women.aspx?sort=popular&pagesize=100',
            'koftyi'        => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/303/women.aspx?sort=popular&pagesize=100',
            'djempera'      => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/302/women.aspx?sort=popular&pagesize=100',
            
            'vechplatya'    => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/1100/women.aspx?sort=popular&pagesize=100',
            'bryuki'        => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/17/women.aspx?sort=popular&pagesize=100',
            'bluzki'        => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/15/women.aspx?sort=popular&pagesize=100',
            'dlinplatya'    => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/2024/page.aspx?sort=popular&pagesize=100',
            
            'kolgotki'      => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/1451/women.aspx?sort=popular&pagesize=100',
            'perchatki'     => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/457/women.aspx?sort=popular&pagesize=100',
            'puhoviki'      => 'http://c.cpl1.ru/4QaL/subaccount/http://www.wildberries.ru/catalog/1541/page.aspx?sort=popular&pagesize=100', 
                
        );
        
        $parts = explode('_', $subid);
        if (count($parts) > 1) {
            if (isset($parts[1]) && isset($links[$parts[1]])) {
                $url = $links[$parts[1]];
                $url = str_replace('subaccount', 'new'.$subid, $url);
                header('Location: '.$url);
                die();
            }    
        }
    }   */
	
	public function print_template($title, $content, $description = null, $page_head = null, $search_query = null)
	{
        $this->_layout = new View($this->_layout);

		$this->_layout->title        = $title;
		$this->_layout->content      = $content;
        $this->_layout->description  = $description;
        $this->_layout->page_head    = $page_head;
        $this->_layout->search_query = $search_query;
		
		$this->response->body($this->_layout);
	}

}
